﻿using gt.webapi.core.Entity.Common;
using gt.webapi.core.Extensions;
using gt.webapi.core.Logging;
using System.Net;
using System.Net.Http;
using System.Web.Http.Filters;

namespace gt.webapi.core.Filters
{
    public class ActionExceptionFilterAttribute : ExceptionFilterAttribute
    {
        private ApiLogger m_logger = null;
        public ActionExceptionFilterAttribute(ApiLogger logger)
        {
            this.m_logger = logger;
        }
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            var exception = actionExecutedContext.Exception;
            if (exception != null)
            {
                actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse<ApiResponse<string>>(
                    HttpStatusCode.OK,
                    new ApiResponse<string>(999, Constants.ERROR_SYSTEM));
                string requestId = actionExecutedContext.Request.GetRequestId();
                this.m_logger.Error(requestId, exception);
            }
        }
    }
}
